package com.asiainfo.dbConnect;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;

/**
 * @Author Administrator
 * @Date 2022/1/27 0027 15:27
 * @Version 1.0
 */
public class DBHelper {
    private static Connection con;
    static {
        try {
            Class.forName(ConfigParams.driver);
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
    }


    /**
     * 获取连接对象
     * @return
     */
    public static Connection getConnection () throws SQLException {
        con =  DriverManager.getConnection(ConfigParams.url,ConfigParams.username,ConfigParams.password);
        return con;
    }

    /**
     *  释放资源
     */
    public static void close(Connection con, ResultSet rs){
        if(con != null){
            try {
                con.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        if(rs != null){
            try {
                rs.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    public static void close(Connection con) {
        close(con,null);
    }
}
